package cn.lanqiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import cn.lanqiao.util.JsfUtil;
import cn.lanqiao.dao.ManagerDao;
import cn.lanqiao.entiy.Manager;
import cn.lanqiao.entiy.Teacher;

public class ManagerDaoImpl implements ManagerDao {
	@Override
	public Manager getManager(Manager manager) {
		Manager reManager = null;
 		Connection conn =JsfUtil.getConnection();
 		String sql="select * from J_MANAGER WHERE muser='"+manager.getMuser()+"' and mpassword='"+manager.getMpassword()+"'";
 		PreparedStatement ps =null;
 		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		try {
			while(rs.next()) {
				String M_ID = rs.getString("M_ID");
				String MUSER = rs.getString("MUSER");
				String MPASSWORD = rs.getString("MPASSWORD");
				System.out.println(M_ID+"-" + MUSER+"-"+MPASSWORD);
				reManager = new Manager(M_ID,MUSER,MPASSWORD);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
 		//6、关闭对象
 		JsfUtil.close(rs, ps, conn);
		return reManager;
	}

	@Override
	public boolean updatePassManager(Manager manager) {
 		Connection conn =JsfUtil.getConnection();
 		String sql="update J_MANAGER set mpassword='"+manager.getMpassword()+"' WHERE muser="+manager.getMuser();
 		System.out.println(sql);
 		PreparedStatement ps =null;
 		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int count=0;
		try {
			count = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean status;
		if(count > 0) {
			status = true;
		}else {
			status = false;
		}
		JsfUtil.close(null, ps, conn);
		return status;
	}

}
